package ms.unicorn.diagram.edit.parts;

import ms.unicorn.Element;
import ms.unicorn.diagram.edit.policies.RelationItemSemanticEditPolicy;
import ms.unicorn.dsl.ILanguageElement;
import ms.unicorn.dsl.LanguageRegistry;
import ms.unicorn.util.FigureUtil;
import ms.unicorn.util.ViewUtil;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITreeBranchEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.draw2d.ui.figures.PolylineConnectionEx;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.View;

/**
 * @generated
 */
public class RelationEditPart extends ConnectionNodeEditPart implements
		ITreeBranchEditPart {

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 4001;

	/**
	 * @generated
	 */
	public RelationEditPart(View view) {
		super(view);
	}

	/**
	 * @generated NOT
	 */
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
				new RelationItemSemanticEditPolicy());
		installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE, new ConnectionEndpointEditPolicy());
	}

	/**
	 * @generated NOT
	 */
	protected boolean addFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof IFigureEditPart
				&& childEditPart.getModel() instanceof View) {
			View view = (View) childEditPart.getModel();
			String childViewName = ViewUtil.getViewName(view);
			IFigure figure = FigureUtil.getChildFigure(getFigure(),
					childViewName);
			if (figure != null) {
				((IFigureEditPart) childEditPart).setEditPartFigure(figure);
				return true;
			}
		}

		/*if (childEditPart instanceof RelationElementTypeEditPart) {
			((RelationElementTypeEditPart) childEditPart)
					.setLabel(getPrimaryShape().getFigureRelationNameFigure());
			return true;
		}*/
		return false;
	}

	/**
	 * @generated
	 */
	protected void addChildVisual(EditPart childEditPart, int index) {
		if (addFixedChild(childEditPart)) {
			return;
		}
		super.addChildVisual(childEditPart, index);
	}

	/**
	 * @generated
	 */
	protected boolean removeFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof RelationElementTypeEditPart) {
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected void removeChildVisual(EditPart childEditPart) {
		if (removeFixedChild(childEditPart)) {
			return;
		}
		super.removeChildVisual(childEditPart);
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model
	 * so you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated NOT
	 */

	protected Connection createConnectionFigure() {
		Element domainElement = (Element) resolveSemanticElement();
		ILanguageElement e = LanguageRegistry.INSTANCE
				.getLanguageElement(domainElement);
		if (e != null) {
			IFigure figure = e.createFigure(domainElement, this);

			if (figure instanceof Connection)
				return (Connection) figure;
		}
		return null;
		//return new RelationFigure();
	}

	/**
	 * @generated
	 */
	public RelationFigure getPrimaryShape() {
		return (RelationFigure) getFigure();
	}

	/**
	 * @generated
	 */
	public class RelationFigure extends PolylineConnectionEx {

		/**
		 * @generated
		 */
		private WrappingLabel fFigureRelationNameFigure;

		/**
		 * @generated
		 */
		public RelationFigure() {

			createContents();
		}

		/**
		 * @generated
		 */
		private void createContents() {

			fFigureRelationNameFigure = new WrappingLabel();

			fFigureRelationNameFigure.setText("<...>");

			this.add(fFigureRelationNameFigure);

		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureRelationNameFigure() {
			return fFigureRelationNameFigure;
		}

	}

}
